home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Developer / Eval / Source / CString.m < prev    next >
Text File  |  1992-01-27  |  1KB  |  66 lines

  1. #import "CString.h"
  2. #import <strings.h>
  3. #import <stdlib.h>
  4.  
  5. // here are the "good" malloc lengths.  Anything larger than
  6. // 4088 is best is a multiple of 8192
  7. int goodLens[] = {16, 32, 64, 128, 176,
  8.      252, 340, 508, 680, 1020, 1360, 2044, 2724, 4088} ;
  9.  
  10. @implementation CString: Object
  11. { char *string ;
  12. }
  13.  
  14.  
  15. + new: (char *) aString ;
  16. { int i,len ;
  17.   self = [[self alloc] init] ;
  18.   // compute best malloc length
  19.   len = strlen(aString) ;
  20.   if(len <= 4088)
  21.   { i = 0 ;
  22.     while(goodLens[i] < len)
  23.     i++ ;
  24.     string = (char *) malloc(goodLens[i]) ;
  25.   }
  26.   else
  27.   { int theLen ;
  28.     i = 1 ;
  29.     while((theLen = (i * 8192)) < len)
  30.       i++ ;
  31.     string = (char *) malloc(theLen) ;
  32.   }
  33.   strcpy(string,aString) ;
  34.   return self ;
  35. }
  36.  
  37. + (int) bestLen: (char *) aString ;
  38. { // return the "best" malloc size for aString
  39.   int len, theLen, i ;
  40.   len = strlen(aString) ;
  41.   if(len <= 4088)
  42.   { i = 0 ;
  43.     while(goodLens[i] < len)
  44.     i++ ;
  45.     return goodLens[i] ;
  46.   }
  47.   else
  48.   { i = 1 ;
  49.     while((theLen = (i * 8192)) < len)
  50.       i++ ;
  51.     return theLen ;
  52.   }
  53. }
  54.  
  55. - free ;
  56. { free(string) ;
  57.   return [super free] ;
  58. }
  59.  
  60. - (const char *) cString ;
  61. { // returns the "address" of the string
  62.   return (const char *) string ;
  63. }
  64.  
  65. @end
  66.